//
//  LastTableViewCell.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/23.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "LastTableViewCell.h"


#define Margin 8

#define LastCellHeight [UIScreen mainScreen].bounds.size.height / 3

@interface LastTableViewCell ()<UITextFieldDelegate>



@end

@implementation LastTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
 
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self loadLastContViewUI];
    }
    return self;
}

- (void)loadLastContViewUI {
    UILabel *verificationCode = [[UILabel alloc] init];
    verificationCode.text = @"验证码";
    verificationCode.textAlignment = NSTextAlignmentLeft;
    verificationCode.font = [UIFont systemFontOfSize:15];
    verificationCode.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(15.0)];
    [self.contentView addSubview:verificationCode];
    
    [verificationCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(Margin *2);
        make.top.equalTo(self.contentView.mas_top).with.offset(Margin * 2);
        make.width.mas_equalTo(WIDTH * 0.3);
        make.height.mas_equalTo(30);
    }];
    
    UITextField *verificationTextField = [[UITextField alloc] init];
    verificationTextField.borderStyle = UITextBorderStyleRoundedRect;
    verificationTextField.font = [UIFont systemFontOfSize:15];
    verificationTextField.keyboardType = UIKeyboardTypeNumberPad;
    [verificationTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [verificationTextField addTarget:self action:@selector(textFieldDidBegin:) forControlEvents:UIControlEventEditingDidBegin];
    self.verificationTextField = verificationTextField;
    self.verificationTextField.delegate = self;
    [self.contentView addSubview:verificationTextField];
    
    [verificationTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(verificationCode.mas_right).with.offset(Margin);
        make.top.equalTo(self.contentView.mas_top).with.offset(Margin * 2);
        make.width.mas_equalTo(WIDTH * 0.2 + 15);
        make.height.mas_equalTo(verificationCode.mas_height);
    }];

    self.Captcha = [[CaptchaView alloc] init];
    NSLog(@"%@",self.Captcha.changeString);
    [self.contentView addSubview:self.Captcha];
    
    [self.Captcha mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(verificationTextField.mas_right).with.offset(Margin);
        make.top.equalTo(self.contentView.mas_top).with.offset(Margin * 2);
        make.width.mas_equalTo(WIDTH * 0.2);
        make.height.mas_equalTo(verificationCode.mas_height);
    }];
    
    
    UILabel *noticeLabel = [[UILabel alloc] init];
    noticeLabel.text = @"注: 为确保你的交易安全，输入验证码前请确认信息";
    noticeLabel.textColor = [UIColor redColor];
    noticeLabel.textAlignment = NSTextAlignmentCenter;
    noticeLabel.font = [UIFont systemFontOfSize:12];
    self.noticeLabel = noticeLabel;
    [self.contentView addSubview:noticeLabel];
    
    [noticeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView.mas_centerX);
        if (iPhone5) {
            make.top.equalTo(verificationTextField.mas_bottom).with.offset(Margin * 3);
        } else if (iPhone6) {
            make.top.equalTo(verificationTextField.mas_bottom).with.offset(Margin * 6);
        } else {
            make.top.equalTo(verificationTextField.mas_bottom).with.offset(Margin * 8);
        }
        make.width.mas_equalTo(WIDTH);
        make.height.mas_equalTo(21);
    }];
    
    UIButton *submitButton = [[UIButton alloc] init];
    submitButton.backgroundColor = DEFAULT_COLOR;
    [submitButton setTitle:@"提 交" forState:UIControlStateNormal];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(didSubmitClickButton:) forControlEvents:UIControlEventTouchUpInside];
    submitButton.layer.cornerRadius = 5;
    submitButton.layer.masksToBounds = YES;
    [self.contentView addSubview:submitButton];
    
    [submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView.mas_centerX);
        make.top.equalTo(noticeLabel.mas_bottom).with.offset(Margin * 3);
        make.width.mas_equalTo(WIDTH - Margin *4);
        make.height.mas_equalTo(40);
    }];
    
}


- (void)textFieldDidChange:(UITextField *)textField
{
    
    if (textField == self.verificationTextField) {
        if (textField.text.length >=4) {
            textField.text = [textField.text substringToIndex:4];
        }
        if (textField.text.length == 4) {
            NSLog(@"调方法");
            
            [self.verificationTextField resignFirstResponder];
        } else {

        }
    }
}

- (void)textFieldDidBegin:(UITextField *)textField{
    
    if (textField.text.length == 4) {
        
        if (![textField.text isEqualToString:self.Captcha.changeString]) {
            self.verificationTextField.text = nil;
        } else {
            
        }
    }
}

- (void)didSubmitClickButton:(UIButton *)submitButton {
    if ([self.delegate respondsToSelector:@selector(didSubmitButtonClick:)]) {
        [self.delegate didSubmitButtonClick:submitButton];
    }
}

@end
